using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;


namespace YF.Controls
{
  public class TrayIcon: Component
  {
    #region Fields



    private readonly NotifyIcon _trayIcon;
    private int _balloonTipTimeout = 0;



    #endregion

    #region Constructors/Finalizers



    public TrayIcon()
    {
      _trayIcon = new NotifyIcon();
      _trayIcon.Visible = true;
    }



    #endregion

    #region Properties/Indexers



    ///<summary>
    ///Gets or sets the icon to display on the balloon tip associated with the <see cref="T:System.Windows.Forms.NotifyIcon"></see>.
    ///</summary>
    ///
    ///<returns>
    ///The <see cref="T:System.Windows.Forms.ToolTipIcon"></see> to display on the balloon tip associated with the <see cref="T:System.Windows.Forms.NotifyIcon"></see>.
    ///</returns>
    ///
    ///<exception cref="T:System.ComponentModel.InvalidEnumArgumentException">The specified value is not a <see cref="T:System.Windows.Forms.ToolTipIcon"></see>.</exception>
    public ToolTipIcon BalloonTipIcon
    {
      get
      {
        return _trayIcon.BalloonTipIcon;
      }
      set
      {
        _trayIcon.BalloonTipIcon = value;
      }
    }

    ///<summary>
    ///Gets or sets the text to display on the balloon tip associated with the <see cref="T:System.Windows.Forms.NotifyIcon"></see>.
    ///</summary>
    ///
    ///<returns>
    ///The text to display on the balloon tip associated with the <see cref="T:System.Windows.Forms.NotifyIcon"></see>.
    ///</returns>
    ///
    public string BalloonTipText
    {
      get
      {
        return _trayIcon.BalloonTipText;
      }
      set
      {
        _trayIcon.BalloonTipText = value;
      }
    }

    [DefaultValue(0)]
    public int BalloonTipTimeout
    {
      get
      {
        return _balloonTipTimeout;
      }
      set
      {
        _balloonTipTimeout = value;
      }
    }

    ///<summary>
    ///Gets or sets the title of the balloon tip displayed on the <see cref="T:System.Windows.Forms.NotifyIcon"></see>.
    ///</summary>
    ///
    ///<returns>
    ///The text to display as the title of the balloon tip.
    ///</returns>
    ///
    public string BalloonTipTitle
    {
      get
      {
        return _trayIcon.BalloonTipTitle;
      }
      set
      {
        _trayIcon.BalloonTipTitle = value;
      }
    }

    ///<summary>
    ///Gets or sets the shortcut menu for the icon.
    ///</summary>
    ///
    ///<returns>
    ///The <see cref="T:System.Windows.Forms.ContextMenu"></see> for the icon. The default value is null.
    ///</returns>
    ///<filterpriority>2</filterpriority>
    public ContextMenu ContextMenu
    {
      get
      {
        return _trayIcon.ContextMenu;
      }
      set
      {
        _trayIcon.ContextMenu = value;
      }
    }

    ///<summary>
    ///Gets or sets the shortcut menu associated with the <see cref="T:System.Windows.Forms.NotifyIcon"></see>.
    ///</summary>
    ///
    ///<returns>
    ///The <see cref="T:System.Windows.Forms.ContextMenuStrip"></see> associated with the <see cref="T:System.Windows.Forms.NotifyIcon"></see>
    ///</returns>
    ///
    public ContextMenuStrip ContextMenuStrip
    {
      get
      {
        return _trayIcon.ContextMenuStrip;
      }
      set
      {
        _trayIcon.ContextMenuStrip = value;
      }
    }


    ///<summary>
    ///Gets or sets the current icon.
    ///</summary>
    ///
    ///<returns>
    ///The <see cref="T:System.Drawing.Icon"></see> displayed by the <see cref="T:System.Windows.Forms.NotifyIcon"></see> component. The default value is null.
    ///</returns>
    ///<filterpriority>1</filterpriority>
    public Icon Icon
    {
      get
      {
        return _trayIcon.Icon;
      }
      set
      {
        _trayIcon.Icon = value;
      }
    }

    ///<summary>
    ///Gets or sets the ToolTip text displayed when the mouse pointer rests on a notification area icon.
    ///</summary>
    ///
    ///<returns>
    ///The ToolTip text displayed when the mouse pointer rests on a notification area icon.
    ///</returns>
    ///
    ///<exception cref="T:System.ArgumentException">ToolTip text is more than 64 characters long.</exception><filterpriority>1</filterpriority><PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" /><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /></PermissionSet>
    public string Text
    {
      get
      {
        return _trayIcon.Text;
      }
      set
      {
        _trayIcon.Text = value;
      }
    }

    ///<summary>
    ///Gets or sets a value indicating whether the icon is visible in the notification area of the taskbar.
    ///</summary>
    ///
    ///<returns>
    ///true if the icon is visible in the notification area; otherwise, false. The default value is false.
    ///</returns>
    ///<filterpriority>1</filterpriority><PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" /><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /></PermissionSet>
    public bool Visible
    {
      get
      {
        return _trayIcon.Visible;
      }
      set
      {
        _trayIcon.Visible = value;
      }
    }



    #endregion

    #region Events



    ///<summary>
    ///Occurs when the balloon tip is clicked.
    ///</summary>
    ///
    public event EventHandler BalloonTipClicked
    {
      add
      {
        _trayIcon.BalloonTipClicked += value;
      }
      remove
      {
        _trayIcon.BalloonTipClicked -= value;
      }
    }

    ///<summary>
    ///Occurs when the balloon tip is closed by the user.
    ///</summary>
    ///
    public event EventHandler BalloonTipClosed
    {
      add
      {
        _trayIcon.BalloonTipClosed += value;
      }
      remove
      {
        _trayIcon.BalloonTipClosed -= value;
      }
    }

    ///<summary>
    ///Occurs when the balloon tip is displayed on the screen.
    ///</summary>
    ///
    public event EventHandler BalloonTipShown
    {
      add
      {
        _trayIcon.BalloonTipShown += value;
      }
      remove
      {
        _trayIcon.BalloonTipShown -= value;
      }
    }

    ///<summary>
    ///Occurs when the user clicks the icon in the notification area.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event EventHandler Click
    {
      add
      {
        _trayIcon.Click += value;
      }
      remove
      {
        _trayIcon.Click -= value;
      }
    }

    ///<summary>
    ///Occurs when the user double-clicks the icon in the notification area of the taskbar.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event EventHandler DoubleClick
    {
      add
      {
        _trayIcon.DoubleClick += value;
      }
      remove
      {
        _trayIcon.DoubleClick -= value;
      }
    }

    ///<summary>
    ///Occurs when the user clicks a <see cref="T:System.Windows.Forms.NotifyIcon"></see> with the mouse.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event MouseEventHandler MouseClick
    {
      add
      {
        _trayIcon.MouseClick += value;
      }
      remove
      {
        _trayIcon.MouseClick -= value;
      }
    }

    ///<summary>
    ///Occurs when the user double-clicks the <see cref="T:System.Windows.Forms.NotifyIcon"></see> with the mouse.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event MouseEventHandler MouseDoubleClick
    {
      add
      {
        _trayIcon.MouseDoubleClick += value;
      }
      remove
      {
        _trayIcon.MouseDoubleClick -= value;
      }
    }

    ///<summary>
    ///Occurs when the user presses the mouse button while the pointer is over the icon in the notification area of the taskbar.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event MouseEventHandler MouseDown
    {
      add
      {
        _trayIcon.MouseDown += value;
      }
      remove
      {
        _trayIcon.MouseDown -= value;
      }
    }

    ///<summary>
    ///Occurs when the user moves the mouse while the pointer is over the icon in the notification area of the taskbar.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event MouseEventHandler MouseMove
    {
      add
      {
        _trayIcon.MouseMove += value;
      }
      remove
      {
        _trayIcon.MouseMove -= value;
      }
    }

    ///<summary>
    ///Occurs when the user releases the mouse button while the pointer is over the icon in the notification area of the taskbar.
    ///</summary>
    ///<filterpriority>1</filterpriority>
    public event MouseEventHandler MouseUp
    {
      add
      {
        _trayIcon.MouseUp += value;
      }
      remove
      {
        _trayIcon.MouseUp -= value;
      }
    }



    #endregion

    #region Methods



    ///<summary>
    ///Displays a balloon tip in the taskbar for the specified time period.
    ///</summary>
    ///
    ///<param name="timeout">The time period, in milliseconds, the balloon tip should display.</param>
    public void ShowBalloonTip(int timeout)
    {
      _trayIcon.ShowBalloonTip(timeout);
    }


    ///<summary>
    ///Displays a balloon tip in the taskbar.
    ///</summary>
    public void ShowBalloonTip()
    {
      _trayIcon.ShowBalloonTip(_balloonTipTimeout);
    }


    ///<summary>
    ///Displays a balloon tip with the specified title, text, and icon in the taskbar for the specified time period.
    ///</summary>
    ///
    ///<param name="tipIcon">One of the <see cref="T:System.Windows.Forms.ToolTipIcon"></see> values.</param>
    ///<param name="timeout">The time period, in milliseconds, the balloon tip should display.</param>
    ///<param name="tipTitle">The title to display on the balloon tip.</param>
    ///<param name="tipText">The text to display on the balloon tip.</param>
    ///<exception cref="T:System.ComponentModel.InvalidEnumArgumentException">tipIcon is not a member of <see cref="T:System.Windows.Forms.ToolTipIcon"></see>.</exception>
    ///<exception cref="T:System.ArgumentOutOfRangeException">timeout is less than 0.</exception>
    ///<exception cref="T:System.ArgumentException">tipText is null or an empty string.</exception>
    public void ShowBalloonTip(int timeout, string tipTitle, string tipText, ToolTipIcon tipIcon)
    {
      _trayIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon);
    }



    #endregion
  }
}